<?php
error_reporting(0);

/**
 * Created by PhpStorm.
 * User: sunshubo
 * Date: 2017/11/28
 * Time: 22:12
 */
class CheckInInfo_model extends MY_Model
{
    function check_in_info($body)
    {
        $body = json_decode($body);
        $latitude = $body->x;
        $longitude = $body->y;
        $id = $body->id;
        $date = date("Y-m-d");
        $time = date("H:i:s", time());
        $year = date("Y");
        $month = date("m");
        $day = date("d");
        $result = null;
        if ($body->type === 'push') {
            $sql = /** @lang text */
                "SELECT STATUS FROM ws_checkin WHERE uid =$id AND DATE ='$date'";
            $query = $this->db->query($sql);
            $rows = $query->num_rows();
            if ($rows) { //场景：没签到，已签退
                foreach ($query->result_array() as $item => $value) {
                    if ($value['STATUS'] === '2' && $rows === 1) {//场景: 未签到,已签退
                        $result = json_encode(array('result' => SUCCESS, 'msg' => '签退成功!'));
                    }

                    if ($value['STATUS'] === '1' && $rows === 1) {//场景: 已签到,未签退
                        $result = $this->check_in($id, $date, $time, $latitude, $longitude, 2, $year, $month, $day);
                    }
                }
                if ($rows === 2) {
                    $result = json_encode(array('result' => SUCCESS, 'msg' => '今天已完成!'));
                }
            } elseif ($rows === 0) {//场景：未签到，未签退
                $result = $this->check_in($id, $date, $time, $latitude, $longitude, 1);
            }
        } elseif ($body->type === 'get') {
            $sql = /** @lang text */
                "SELECT STATUS,TIME FROM ws_checkin WHERE uid =$id AND DATE ='$date'";
            $query = $this->db->query($sql);
            $rows = $query->num_rows();
            $data = array('count' => $rows, 'data' => array(), 'result' => SUCCESS);
            if ($query) {
                foreach ($query->result_array() as $item => $value) {
                    $gd = array();
                    $gd['s'] = $value['STATUS'];
                    $gd['t'] = $value['TIME'];
                    array_push($data['data'], $gd);
                }
            }
            $result = json_encode($data);
        } elseif ($body->type === 'list') {
            $like = $body->like;
            $sql = /** @lang text */
//                "SELECT STATUS,TIME,year,month,day FROM ws_checkin WHERE uid =$id AND DATE LIKE '$like%'";
                "SELECT DAY,STATUS,COUNT(DATE)AS COUNT FROM ws_checkin WHERE DATE LIKE '$like%' AND uid=$id GROUP BY DATE HAVING DATE";
            $query = $this->db->query($sql);
            $rows = $query->num_rows();
            $data = array('count' => $rows, 'data' => array(), 'result' => SUCCESS);
            if ($query) {
                foreach ($query->result_array() as $item => $value) {
                    $gd = array();
                    $gd['s'] = $value['STATUS'];
                    $gd['c'] = $value['COUNT'];
                    $gd['d'] = $value['DAY'];
                    array_push($data['data'], $gd);
                }
            }
            $result = json_encode($data);
        }
        return $result;
    }

    function check_in($uid, $date, $time, $latitude, $longitude, $status, $year, $month, $day)
    {
        $sql = /** @lang text */
            "INSERT INTO ws_checkin(uid,date,time,status,pos_latitude,pos_longitude,year,month,day) VALUE($uid,'$date','$time',$status,$latitude,$longitude,$year,$month,$day)";

        $query = $this->db->query($sql);
        $error = $this->db->error();
        $result = null;
        if ($query) {
            if ($status === 1) {
                $result = json_encode(array('result' => SUCCESS, 'msg' => '签到成功!'));
            } elseif ($status === 2) {
                $result = json_encode(array('result' => SUCCESS, 'msg' => '签退成功!'));
            }
        }
        return $result;
    }
}